//
//  NewMessageTabelViewCell.m
//  ExcellentHome
//
//  Created by Andy on 16/5/31.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "NewMessageTabelViewCell.h"
#import "MessageModel.h"


#import <UIImageView+WebCache.h>
#import "UIImageView+SEGWebCache.h"

#define kUIScreenWidth [UIScreen mainScreen].bounds.size.width
@implementation NewMessageTabelViewCell
//-(BaseImageEnarge *)baseImage{
//    if (!_baseImage) {
//        _baseImage = [BaseImageEnarge new];
//    }
//    return _baseImage;
//}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        /**<背景层*/
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, kUIScreenWidth,20)];
        self.bgView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.bgView];
        
        /**<头像*/
        UIImage *headImage = [UIImage imageWithContentsOfFile:SEGPicName(@"AppIcon60")];
        self.headerImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 0, 40, 40)];
        self.headerImage.layer.cornerRadius = 2.f;
        [self.headerImage.layer setMasksToBounds:YES];
        [self.headerImage setImage:headImage];
        [self.bgView addSubview:self.headerImage];
        
        
        /**<内容框*/
        self.content = [[UIView alloc] initWithFrame:CGRectMake(self.headerImage.frame.origin.x+CGRectGetWidth(self.headerImage.bounds)+5, 0, kUIScreenWidth-86, CGRectGetHeight(self.bgView.bounds))];
        self.content.backgroundColor = [UIColor clearColor];
        self.content.layer.cornerRadius = 5.0f;
        [self.content.layer setMasksToBounds:YES];
        [self.bgView addSubview:self.content];
        
        
        /**<非自己汽泡泡三角*/
        self.image = [UIImageView new];
        [self.content addSubview:self.image];
        
        self.imgView = [UIImageView new];
        [self.content addSubview:self.imgView];
        /**<标题*/
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 16, CGRectGetWidth(self.content.bounds), 20)];
        [self.titleLabel setTextColor:kColor_1];
        [self.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [self.titleLabel setText:@""];
        self.contentLabel.lineBreakMode = NSLineBreakByClipping;
        self.contentLabel.numberOfLines = 0;
        self.titleLabel.backgroundColor = [UIColor clearColor];
        [self.content addSubview:self.titleLabel];
        
        /**<时间*/
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, self.titleLabel.frame.origin.y+CGRectGetHeight(self.titleLabel.bounds), CGRectGetWidth(self.content.bounds), 20)];
        [self.timeLabel setTextColor:kColor_2];
        [self.timeLabel setFont:[UIFont systemFontOfSize:10]];
        [self.timeLabel setText:@""];
        self.timeLabel.backgroundColor = [UIColor clearColor];
        [self.content addSubview:self.timeLabel];
        
        
        /**<内容*/
        self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, self.timeLabel.frame.origin.y+CGRectGetHeight(self.timeLabel.bounds)+5, CGRectGetWidth(self.content.bounds)-30, 20)];
        self.contentLabel.lineBreakMode = NSLineBreakByClipping;
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.backgroundColor = [UIColor clearColor];
        [self.contentLabel setFont:[UIFont systemFontOfSize:16]];
        [self.content addSubview:self.contentLabel];
        
        self.featuresLable = [[UILabel alloc] initWithFrame:CGRectMake(15, self.contentLabel.frame.origin.y+CGRectGetHeight(self.contentLabel.bounds)+5, CGRectGetWidth(self.content.bounds)-30, 0.1)];
        [self.featuresLable setFont:[UIFont systemFontOfSize:16]];
        [self.featuresLable setTextColor:kColor_S3];
        self.featuresLable.lineBreakMode = NSLineBreakByClipping;
        self.featuresLable.numberOfLines = 0;
        self.featuresLable.backgroundColor = [UIColor clearColor];
        [self.content addSubview:self.featuresLable];
        
        //图片
        self.imageContent = [UIImageView new];
        
        [self.content addSubview:self.imageContent];
        
        self.content.frame = CGRectMake(self.content.frame.origin.x,
                                       self.content.frame.origin.y,
                                       CGRectGetWidth(self.content.bounds),
                                       20+CGRectGetHeight(self.bgView.bounds));
        
        self.bgView.frame = CGRectMake(self.bgView.frame.origin.x,
                                  self.bgView.frame.origin.y,
                                  CGRectGetWidth(self.bgView.bounds),
                                  CGRectGetHeight(self.content.bounds));
    }
    
    return self;
}

-(void)onRequestList:(id)data
{
    MessageModel *message = data;
    //头像
    if ([ValueToString(message.sendUserId) isEqualToString:kAccountUserID]) {
        self.sysAccount = [SEGProfileManager sharedManager].profile;
        [self.headerImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,self.sysAccount.ico]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg40")] options:SDWebImageLowPriority];
        
//        NSString *filePath = [SEGUtils mideaFilePathForFileName:self.sysAccount.SDWebImageFromCacheOnly];
//        self.headerImage.image = [UIImage imageWithContentsOfFile:filePath];
//        if (!self.headerImage.image) {
//            self.headerImage.image = [UIImage imageWithContentsOfFile:SEGPicName(@"headimg40")];
//        }
        self.headerImage.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 15 - 40, 0, 40, 40);
    }else{
        [self.headerImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,message.usericon]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg40")] options:SDWebImageRetryFailed];
        self.headerImage.frame = CGRectMake(15, 0, 40, 40);
    }

    if ([message.contenttype  isEqualToString:@"2"]) {//图片
        self.titleLabel.text = nil;
        self.timeLabel.text = nil;
        self.featuresLable.text = nil;
        self.contentLabel.text = nil;
        //显示图片

        NSData *data = [UIImageView saveServiceImage:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,message.message]]];
            if (data) {
                self.imageContent.image = [UIImage imageWithData:data];
            }else{
                self.imageContent.image = [UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_260_390")];
            }

        if ([ValueToString(message.sendUserId) isEqualToString:kAccountUserID]){


            self.imageContent.frame = CGRectMake(0,
                                                 0,
                                                 self.imageContent.image.size.width/(self.imageContent.image.size.height/90),
                                                 90 );
            self.imageContent.contentMode = UIViewContentModeScaleAspectFit;

            self.content.frame = CGRectMake(
                                            self.headerImage.frame.origin.x - self.imageContent.frame.size.width - 5,
                                            self.content.frame.origin.y,
                                            self.imageContent.frame.size.width,
                                            self.imageContent.frame.size.height);

            self.imageContent.layer.cornerRadius = 5.0f;
            [self.imageContent.layer setMasksToBounds:YES];


        }else{
            self.imageContent.frame = CGRectMake(0, 0, self.imageContent.image.size.width/(self.imageContent.image.size.height/90), 90 );
            self.imageContent.contentMode = UIViewContentModeScaleAspectFit;
            self.content.frame = CGRectMake(
                                            self.headerImage.frame.origin.x + self.headerImage.frame.size.width + 5,
                                            self.content.frame.origin.y,
                                            self.imageContent.frame.size.width,
                                            self.imageContent.frame.size.height);


            self.imageContent.layer.cornerRadius = 5.0f;
            [self.imageContent.layer setMasksToBounds:YES];

        }
    }else
    {//文本
        self.imageContent.image = nil;
        self.titleLabel.text = nil;
        self.timeLabel.text = nil;
        self.featuresLable.text = nil;
        self.contentLabel.text = nil;
        self.imageContent.image = nil;


        self.contentLabel.attributedText = [self attributedString:message.message];

        CGSize size = [self setupLabel:message.message flo:16];

        if ([ValueToString(message.sendUserId) isEqualToString:kAccountUserID]){

            self.contentLabel.frame = CGRectMake(10, self.content.frame.origin.y +8, size.height>20?kUIScreenWidth-140:size.width, size.height);
//            [self.contentLabel sizeToFit];
            [self.contentLabel setTextColor:kColor_W];

            self.content.frame = CGRectMake(self.headerImage.frame.origin.x - self.contentLabel.frame.size.width - 35, self.content.frame.origin.y,
                                            self.contentLabel.frame.size.width + 30,
                                            size.height + 10>45?size.height + 20:45);
            self.contentLabel.center = CGPointMake(self.contentLabel.center.x, self.content.center.y);

        }else{
            if ([message.turnType isEqualToString:@"0"]) {
                self.contentLabel.frame = CGRectMake(20, self.content.frame.origin.y +8, size.height>20?kUIScreenWidth-140:size.width, size.height);
//                [self.contentLabel sizeToFit];
                self.content.frame = CGRectMake(self.headerImage.frame.origin.x+45,
                                                self.content.frame.origin.y,
                                                self.contentLabel.frame.size.width + 30,
                                                self.contentLabel.frame.origin.y + self.contentLabel.frame.size.height +16 );
            }
            else{
                [self.titleLabel setText:ValueToString(message.title)];
                self.titleLabel.frame = CGRectMake(20, 8, self.titleLabel.frame.size.width, self.titleLabel.frame.size.height);

                [self.timeLabel setText:ValueToString(message.createTime)];
                self.timeLabel.frame = CGRectMake(20, self.titleLabel.frame.origin.y + self.titleLabel.frame.size.height +8, self.timeLabel.frame.size.width, self.timeLabel.frame.size.height);



                self.contentLabel.frame = CGRectMake(20, self.timeLabel.frame.origin.y + self.timeLabel.frame.size.height +15, size.height>20?kUIScreenWidth-140:size.width, size.height);

                if ([message.turnTip isEqualToString:@"0"]) {
                    self.featuresLable.text = @"";
                    [self.featuresLable sizeToFit];
                    self.featuresLable.frame = self.contentLabel.frame;
                }else{
                    self.featuresLable.attributedText = [self attributedString:message.turnTip];

                    self.featuresLable.frame = CGRectMake(20,
                                                          self.contentLabel.frame.origin.y + self.contentLabel.frame.size.height +10,
                                                          size.width,
                                                          self.featuresLable.frame.size.height);
                    [self.featuresLable sizeToFit];
                }

                self.content.frame = CGRectMake(self.headerImage.frame.origin.x+45,
                                                self.content.frame.origin.y,
                                                self.contentLabel.frame.size.width + 30,
                                                self.featuresLable.frame.origin.y + self.featuresLable.frame.size.height +16 );

            }
        }
    }
    //气泡
    self.imgView.image = [ValueToString(message.sendUserId) isEqualToString:kAccountUserID]?[[UIImage imageWithContentsOfFile:SEGPicName(@"right_chat_bubble")] stretchableImageWithLeftCapWidth:15 topCapHeight:35]: [[UIImage imageWithContentsOfFile:SEGPicName(@"left_chat_bubble")] stretchableImageWithLeftCapWidth:15 topCapHeight:35];
    if (self.imageContent.image) {//图片气泡角
        self.content.backgroundColor = [UIColor clearColor];
        CALayer *layer              = self.imgView.layer;
        layer.frame                 = (CGRect){{0,0},self.imgView.layer.frame.size};
        self.imageContent.layer.mask = layer;
        [self.imageContent setNeedsDisplay];
    }else{
        if ([ValueToString(message.sendUserId) isEqualToString:kAccountUserID]) {
            self.content.backgroundColor = kColor_T;
        }else{
            self.content.backgroundColor = [UIColor whiteColor];
        }

        CALayer *layer             = self.imgView.layer;
        layer.frame                 = (CGRect){{0,0},self.imgView.layer.frame.size};
        self.content.layer.mask = layer;
        [self.content setNeedsDisplay];
    }
    self.imgView.frame = self.content.bounds;
    //改变外层框架
    self.bgView.frame = CGRectMake(self.bgView.frame.origin.x,
                                   self.bgView.frame.origin.y,
                                   CGRectGetWidth(self.bgView.bounds),
                                   CGRectGetHeight(self.content.bounds));
//    self.timeLabel.backgroundColor = [UIColor redColor];
//    self.titleLabel.backgroundColor = [UIColor purpleColor];
//    self.contentLabel.backgroundColor = [UIColor blueColor];
//    self.featuresLable.backgroundColor = [UIColor redColor];
//    self.content.backgroundColor = [UIColor orangeColor];
//    self.bgView.backgroundColor = [UIColor lightGrayColor];
}

-(NSMutableAttributedString *)attributedString:(NSString *)message
{
    NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithData:[message dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    [attributedString addAttribute:NSFontAttributeName
                              value:[UIFont systemFontOfSize:16]
                              range:NSMakeRange(0, [attributedString length])];
    return attributedString;
}

-(CGFloat)cellHight:(id)data
{
    MessageModel *message = data;
    CGSize size5 = [self setupLabel:message.message flo:16];
    if ([ValueToString(message.sendUserId) isEqualToString:kAccountUserID]) {
        if ([message.contenttype  isEqualToString:@"2"] ) {
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,message.message]];
            NSData *data = [UIImageView saveServiceImage:url];
            if (data) {
                self.imageContent.image = [UIImage imageWithData:data];
            }else{
                self.imageContent.image = [UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_260_390")];
            }
            self.imageContent.frame = CGRectMake(0,
                                                 0,
                                                 self.imageContent.image.size.width/(self.imageContent.image.size.height/90),
                                                 90 );
            self.imageContent.contentMode = UIViewContentModeScaleAspectFit;
            return self.imageContent.frame.size.height+20?self.imageContent.frame.size.height+20:65;
        }
        return size5.height>25?size5.height+40:65;
    }else{

        CGSize size4 = [self setupLabel:message.turnTip flo:16];
        if ([message.turnType isEqualToString:@"0"]) {
            if ([message.contenttype  isEqualToString:@"2"] ) {
                [self.imageContent setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,message.message]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_260_390")] options:SDWebImageRetryFailed];
                self.imageContent.frame = CGRectMake(0,
                                                         0,
                                                         self.imageContent.image.size.width/(self.imageContent.image.size.height/90),
                                                         90);
                self.imageContent.contentMode = UIViewContentModeScaleAspectFit;
                return self.imageContent.image.size.height+20?self.imageContent.frame.size.height+20:65;
            }
            return size5.height>25?size5.height+40:65;
        }

        return size4.height +size5.height + 102;
    }
    return 220;
}

- (CGSize)setupLabel:(NSString *)str flo:(CGFloat)fla{

    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.font = [UIFont systemFontOfSize:16];
    textLabel.backgroundColor = [UIColor redColor];
    textLabel.text = str;
    textLabel.numberOfLines = 0;//根据最大行数需求来设置
    textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    CGSize maximumLabelSize = CGSizeMake(kUIScreenWidth-140, 999);//labelsize的最大值
    //关键语句
    CGSize expectSize = [textLabel sizeThatFits:maximumLabelSize];
    
    return expectSize;
}

- (void)turnType:(NSString *)str{
    switch ([str integerValue]) {
        case 0:
            self.featuresLable.text = nil;
            break;
        case 10:
			self.featuresLable.text = @"去支付";
            break;
        case 20:
			self.featuresLable.text = @"申请退款";
            break;
        case 30:
			self.featuresLable.text = @"去评价";
            break;
        case 40:
			self.featuresLable.text = @"申请退款成功";
            break;
        case 50:
            self.featuresLable.text = @"退款成功";
            break;
        case 60:
            self.featuresLable.text = @"再次购买";
            break;
        case 70:
            self.featuresLable.text = @"评价满意";
            break;
        case 80:
            self.featuresLable.text = @"评价不满意";
            break;

            
            
        default:
            self.featuresLable.text = @"评价不满意";
            break;
    }
}

@end
